﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows.Media;

namespace Candy.Model
{
    public static class ExtensionMethods
    {
        public static ObservableCollection<T> ToObservableCollection<T>(this IEnumerable<T> collection)
        {
            ObservableCollection<T> ret = new ObservableCollection<T>();
            foreach (var item in collection)
            {
                ret.Add(item);
            }
            return ret;
        }

        public static Color ToColor(this string colorString)
        {
            string col = colorString.Replace("#", "");
            if (col.Length == 6 || col.Length == 8)
            {

                int alphaOffset = 0;
                byte a = 255;
                byte r = 255;
                byte g = 255;
                byte b = 255;
                if (col.Length == 8)
                {
                    alphaOffset = 2;
                    a = Convert.ToByte(col.Substring(0, 2), 16);
                }
                if (col.Length == 6)
                {
                    r = Convert.ToByte(col.Substring(0 + alphaOffset, 2), 16);
                    g = Convert.ToByte(col.Substring(2 + alphaOffset, 2), 16);
                    b = Convert.ToByte(col.Substring(4 + alphaOffset, 2), 16);
                }
                return Color.FromArgb(a, r, g, b);

            }
            else
                throw new ArgumentException("Argument has wrong format");
        }

        public static SolidColorBrush ToBrush(this string colorString)
        {
            return new SolidColorBrush(colorString.ToColor());
        }
    }
}
